package org.caenv.interpreter.pojo.instructions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import org.caenv.interpreter.parser.MemoryManager;
import org.caenv.interpreter.pojo.instructions.comparator.Comparator;

public class ExecutionPlan {

    private State currentState;
    private final Map<String, State> statesMap;
    private final Stack<Comparator> comparatorStack;
    private final MemoryManager memoryManager;

    public ExecutionPlan() {
        this.statesMap = new HashMap<String, State>();
        this.comparatorStack = new Stack<Comparator>();
        this.memoryManager = new MemoryManager();
        this.comparatorStack.push(new Comparator()); // Comparator 1 == 1
    }

    public void addState(State state) {
        this.statesMap.put(state.getName(), state);
    }

    public Comparator getComparator() {
        return this.comparatorStack.peek();
    }

    public void pushComparator(Comparator comparator) {
        this.comparatorStack.push(comparator);
    }

    public Comparator popComparator() {
        return this.comparatorStack.pop();
    }

    public List<State> getStates() {
        return new ArrayList<State>(this.statesMap.values());
    }

    public State getStateByName(String name) {
        return this.statesMap.get(name);
    }

    public boolean setStateByName(String name) {
        if (this.statesMap.containsKey(name)) {
            this.currentState = this.statesMap.get(name);
            return true;
        } else {
            System.err.println("estado " + name + " nao encontado! [" + this.statesMap.keySet() + "]");
            return false;
        }
    }

    public State getCurrentState() {
        return currentState;
    }

    public MemoryManager getMemoryManager() {
        return this.memoryManager;
    }
    
}
